package handler

import (
	"context"
	"errors"
	"fmt"
	"shop/common/global"
	"shop/common/model"
	"shop/common/pkg"
	"shop/user_grpc/proto/user"
	"time"
)

type Server struct {
	user.UnimplementedUserServer
}

func (s *Server) Register(_ context.Context, in *user.RegisterRequest) (*user.RegisterResponse, error) {
	var users model.User
	global.DB.Where("account = ?", in.Account).Find(&users)
	if users.Id != 0 {
		return nil, errors.New("当前账号已存在")
	}
	result, _ := global.Rdb.Get(global.Ctx, fmt.Sprintf("sendSms%s", in.Account)).Result()
	fmt.Println(result)
	fmt.Println(in.Code)
	if in.Code != result {
		return nil, errors.New("验证码错误")
	}
	users.Account = in.Account
	users.Password = pkg.Md5(in.Password)
	global.DB.Create(&users)
	return &user.RegisterResponse{
		Id: int64(users.Id),
	}, nil
}
func (s *Server) Login(_ context.Context, in *user.LoginRequest) (*user.LoginResponse, error) {
	var users model.User
	global.DB.Where("account = ?", in.Account).Find(&users)
	if users.Id == 0 {
		return nil, errors.New("当前账号不存在")
	}
	if in.Password == "" {
		result, _ := global.Rdb.Get(global.Ctx, fmt.Sprintf("sendSms%s", in.Account)).Result()
		if result == "" {
			return nil, errors.New("请先获取验证码")
		}
		if in.Code != result {
			return nil, errors.New("验证码错误")
		}
	} else {
		if pkg.Md5(in.Password) != users.Password {
			return nil, errors.New("密码错误")
		}
	}
	//一个账号同时只能在线3个设备
	var userd []model.UserDevice
	global.DB.Where("user_id = ?", users.Id).Find(&userd)
	if len(userd) >= 3 {
		global.DB.Where("user_id = ?", users.Id).Order("update_time asc").Limit(1).Delete(&model.UserDevice{})
	}
	handler, _ := pkg.TokenHandler(int(users.Id))
	global.Rdb.Set(global.Ctx, in.Account, handler, time.Hour*24)
	u0 := model.UserDevice{
		UserId: int32(users.Id),
		Token:  handler,
	}
	global.DB.Create(&u0)
	return &user.LoginResponse{
		Id: int64(users.Id),
	}, nil
}
func (s *Server) FindMe(_ context.Context, in *user.FindMeRequest) (*user.FindMeResponse, error) {
	var users model.User
	global.DB.Where("account = ?", in.Account).Find(&users)
	if users.Id == 0 {
		return nil, errors.New("当前账号不存在")
	}
	result, _ := global.Rdb.Get(global.Ctx, fmt.Sprintf("sendSms%s", in.Account)).Result()
	if result == "" {
		return nil, errors.New("请先获取验证码")
	}
	if in.Code != result {
		return nil, errors.New("验证码错误")
	}
	users.Account = in.Account
	users.Password = pkg.Md5(in.Password)
	global.DB.Where("id=?", users.Id).Updates(&users)
	return &user.FindMeResponse{
		Id: int64(users.Id),
	}, nil
}

func (s *Server) CreateAddress(_ context.Context, in *user.CreateAddressRequest) (*user.CreateAddressResponse, error) {
	var address model.Address
	//判断status是否为默认
	if in.Status == 2 {
		global.DB.Where("user_id = ? and status = ?", in.UserId, in.Status).Find(&address)
		if address.Id != 0 {
			global.DB.Model(model.Address{}).Where("user_id = ? and status = ?", in.UserId, in.Status).Update("status", 1)
		}
	}
	address = model.Address{
		Phone:  in.Phone,
		Area:   in.Area,
		Status: in.Status,
		UserId: in.UserId,
	}
	global.DB.Create(&address)
	return &user.CreateAddressResponse{}, nil
}

func (s *Server) UserColl(_ context.Context, in *user.UserCollRequest) (*user.UserCollResponse, error) {
	var coll model.Coll
	global.DB.Where("user_id = ? and goods_id = ?", in.UserId, in.GoodId).Find(&coll)
	if coll.Id == 0 {
		coll.UserId = in.UserId
		coll.GoodsId = in.GoodId
		global.DB.Create(&coll)
	} else {
		global.DB.Delete(&coll)
	}
	return &user.UserCollResponse{}, nil
}
